package com.zimug.dongbb.server.jwt.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zimug.dongbb.persistence.system.mapper.MySystemMapper;
import com.zimug.dongbb.persistence.system.mapper.SysUserMapper;
import com.zimug.dongbb.persistence.system.model.SysUser;
import com.zimug.dongbb.persistence.system.model.SysUserOrg;
import com.zimug.dongbb.server.jwt.config.DbLoadSysConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private MySystemMapper mySystemMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private DbLoadSysConfig dbLoadSysConfig;

    //根据登录用户名查询用户信息
    public SysUser getUserByUserName(String userName){
      Assert.isTrue(StringUtils.isNotEmpty(userName),
        "查询参数用户名不存在");

      SysUser sysUser = sysUserMapper.selectOne(
        new QueryWrapper<SysUser>().eq("username",userName));
      if(sysUser != null){
        sysUser.setPassword("");  //清空密码信息
      }
      return sysUser;
    }

    //用户管理：查询
    public IPage<SysUserOrg> queryUser(Long orgId ,
                                      String username ,
                                      String phone,
                                      String email,
                                      Boolean enabled,
                                      Date createStartTime,
                                      Date createEndTime,
                                      Integer pageNum,
                                      Integer pageSize){
      Page<SysUserOrg> page = new Page<> (pageNum,pageSize);   //查询第pageNum页，每页pageSize条数据
      return mySystemMapper.selectUser(
                                    page,
                                    orgId,username,phone,email,enabled,
                                    createStartTime,
                                    createEndTime);
    }

  //用户管理：修改
  public void updateUser(SysUser sysuser){
    Assert.isTrue(sysuser.getId() != null,
      "修改操作必须带主键");

    sysUserMapper.updateById(sysuser);
  }

  //用户管理：新增
  public void addUser(SysUser sysuser){
    sysuser.setPassword(passwordEncoder.encode(
      dbLoadSysConfig.getConfigItem("user.init.password")
    ));
    sysuser.setCreateTime(new Date());  //创建时间
    sysuser.setEnabled(true); //新增用户激活
    sysUserMapper.insert(sysuser);
  }

  //用户管理：删除
  public void deleteUser(Long userId){
    Assert.isTrue(userId != null, "删除操作必须带主键");
    sysUserMapper.deleteById(userId);
  }

  //用户管理：重置密码
  public void pwdreset(Long userId){
    Assert.isTrue(userId != null, "重置密码操作必须带主键");

    SysUser sysUser = new SysUser();
    sysUser.setId(userId);
    sysUser.setPassword(passwordEncoder.encode(
      dbLoadSysConfig.getConfigItem("user.init.password")
    ));
    sysUserMapper.updateById(sysUser);
  }

  //判断当前登录的用户密码是否是默认密码，如果是，会让他去修改
  public Boolean isdefault(String username){
    SysUser sysUser = sysUserMapper.selectOne(
      new QueryWrapper<SysUser>().eq("username",username));

    //判断数据库密码是否是默认密码
    return passwordEncoder.matches(
      dbLoadSysConfig.getConfigItem("user.init.password"),
      sysUser.getPassword());
  }

  //个人中心：修改密码
  public void changePwd(String username,String oldPass,String newPass){

    SysUser sysUser = sysUserMapper.selectOne(
      new QueryWrapper<SysUser>().eq("username",username));
    //判断旧密码是否正确
    boolean isMatch = passwordEncoder.matches(oldPass,sysUser.getPassword());
    Assert.isTrue(isMatch, "原密码输入错误，请确认后重新输入！");

    SysUser sysUserNew = new SysUser();
    sysUserNew.setId(sysUser.getId());
    sysUserNew.setPassword(passwordEncoder.encode(newPass));
    sysUserMapper.updateById(sysUserNew);
  }

  //用户管理：更新用户的激活状态
  public void updateEnabled(Long id,Boolean enabled){
    Assert.isTrue(id != null, "修改操作必须带主键");
    SysUser sysUser = new SysUser();
    sysUser.setId(id);
    sysUser.setEnabled(enabled);
    sysUserMapper.updateById(sysUser);
  }

}
